﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jxym.DBUtility;
using Jxym.wlkc2011.Module.CMS.MODEL;
using System.Data.SqlClient;
using System.Collections;

namespace Jxym.wlkc2011.Module.CMS.SERVER
{
    public partial class Cms_ContentClassSort : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string nodeId = Request.Params["nodeId"];
            string oldNodeId = Request.Params["oldNodeId"];
            string newNodeId = Request.Params["newNodeId"];
            int index = int.Parse(Request.Params["index"]);

            MsSqlDal<Cms_ContentClass> iUtm = new MsSqlDal<Cms_ContentClass>();

            Cms_ContentClass node = iUtm.GetModel(nodeId);
            node.Sort = index;

            SqlParameter[] params2 = new SqlParameter[] { 
                new SqlParameter("@SiteName",node.SiteName),
                new SqlParameter("@ParentID", newNodeId.Equals("TreeRoot") ? "0" : newNodeId) 
            };

            IList<Cms_ContentClass> newNodes = iUtm.GetList("ParentID=@ParentID AND SiteName=@SiteName", params2, "Sort", 0);

            bool isTjjd = false;

            for (int i = 0; i < newNodes.Count; i++)
            {
                Cms_ContentClass n = newNodes[i];

                if (node.ID.Equals(n.ID))
                {
                    isTjjd = true;
                    break;
                }
            }

            if (!isTjjd)
            {
                newNodes.Add(node);
            }

            for (int i = 0; i < newNodes.Count; i++)
            {
                Cms_ContentClass n = newNodes[i];

                if (i >= index)
                {
                    n.Sort = i + 1;
                }
                else if (n.ID == node.ID)
                {
                    n.Sort = index;
                }
                else
                {
                    n.Sort = i;
                }

                n.ParentID = newNodeId.Equals("TreeRoot") ? 0 : Convert.ToInt32(newNodeId);

                SqlParameter[] param3 = new SqlParameter[] {
                    new SqlParameter("@ID",n.ID),
                    new SqlParameter("@Sort",n.Sort),
                    new SqlParameter("@ParentID",n.ParentID)
                };
                iUtm.Updata("ID=@ID", param3);
            }
        }
    }
}